#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=tt2go\tt2go_icon.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Comment=AutoRun file for tt2go
#AutoIt3Wrapper_Res_Description=tt2go autorun
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Project:        Tech Tools To Go
 Author:         Joe Vasquez

#ce ----------------------------------------------------------------------------
; Includes
#include <Inet.au3>

; Program Options
$timedelay = 250
$ini_file = @ScriptDir & "\tt2go\tt2go.ini"

; Check for command line options
If $CmdLine[0]=1 Then
	If $CmdLine[1] = "/noupdate" Then
		_launch_pstart()
		Exit
	ElseIf $CmdLine[1] = "/forceupdate" Then
		; Check if pstart is running
		If ProcessExists("pstart.exe") Then ProcessClose("pstart.exe")
		_launch_ketarin()
		_launch_pstart()
		Exit
	EndIf
EndIf

; Before we do anything with Ketarin, lets see if we already updated today
If FileExists($ini_file) Then
	; Read last update date from ini file
	$iniread_last_update = IniRead($ini_file, "Config", "Last Update", 0)
	
	; Check if we need an update
	$today_date = @YEAR & @MON & @MDAY
	If ($iniread_last_update >= $today_date) Then 
		_launch_pstart()
		Exit
	EndIf
EndIf

; Call the _launch_ketarin funciton
_launch_ketarin()

; Now that we are updated lets launch the menu
_launch_pstart()

Func _Download_With_Progress($url, $path, $title)
	If InetGetSize($url) > 0 Then
		ProgressOn("Downloading", $title, "0 percent")
		$Size = InetGetSize($url)
		$Download = InetGet($url, $path, 1, 1)
		While @InetGetActive = 1
			$P = Round((@InetGetBytesRead / $Size) * 100, 0)
			$KBS = 0
			$Before = @InetGetBytesRead
			$Timer = TimerInit()
			Do
				Sleep(10)
			Until TimerDiff($Timer) >= 1000
			$KBS = Round((@InetGetBytesRead - $Before) / 1024, 1)
			ProgressSet($P, $P & "% @ " & $KBS & " KB/S")
			Sleep(10)
		WEnd
		ProgressSet(100, "Done", "Complete")
		Sleep(500)
		ProgressOff()
		Return True
	Else
		Return False
	EndIf
EndFunc   ;==>_Download_With_Progress

Func _launch_pstart()
	Run(@ScriptDir & "\tt2go\pstart.exe")
EndFunc

Func _launch_ketarin()
	; Check for .NET Framework before lunching Ketarin
	If (Not FileExists(@WindowsDir & "\Microsoft.Net\Framework")) Then
		MsgBox(0, "Missing .Net", "Missing .Net Framework")
		$net_framework_url = "http://download.microsoft.com/download/6/0/f/60fc5854-3cb8-4892-b6db-bd4f42510f28/dotnetfx35.exe"
		_Download_With_Progress($net_framework_url, @TempDir & "\dotnetfx35.exe", "Microsoft .Net Framework v3.5")
		While @InetGetActive = 1
			Sleep($timedelay)
		WEnd
		RunWait(@TempDir & "\dotnetfx35.exe /qb /norestart")
	EndIf

	; Update the ini file with update date
	IniWriteSection($ini_file, "Config", "Last Update=" & @YEAR & @MON & @MDAY)
	
	; Now lets lunch Ketarin and update
	RunWait(@ScriptDir & "\tt2go\ketarin.exe /silent /notify")
EndFunc